
 package com.glgc.projecti.web.filter;

import java.io.IOException;
import java.util.Enumeration;
import java.util.ResourceBundle;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.glgc.projecti.common.CommonConstant;
import com.glgc.projecti.common.CommonUtils;

public class LoginFilter extends HttpServlet implements Filter {
	private Logger logger = Logger.getLogger(LoginFilter.class);
	private static final long serialVersionUID = -6687590090464076360L;

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain filterChain) throws IOException, ServletException {
		
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;
		HttpSession session = req.getSession();
		String path = req.getContextPath();
		String basePath = request.getScheme() + "://" + request.getServerName()
				+ ":" + request.getServerPort() + path + "/";
		
		String requestPath = req.getServletPath();
		
		// 进入后台loginFilter
		if ((requestPath.indexOf("html") != -1 ||
				requestPath.indexOf("index.jsp") != -1)
				&& requestPath.indexOf("pc") == -1 
				&& requestPath.indexOf("announce.html") == -1 
				&& session.getAttribute(CommonConstant.LOGIN_NAME) == null) {
			res.sendRedirect(basePath + "serverLogin.jsp"); 
			return;
		}
		
		/*
		session.removeAttribute(CommonConstant.CLIENT_LOGIN_NAME);
				
		if (session.getAttribute(CommonConstant.CLIENT_LOGIN_NAME) == null) {
			ClientLoginer login = new ClientLoginer();
			login.setFundAccount("1");
			login.setMobileTel("111");
			login.setContestantId("1");
			login.setSourceFlag("1");
			login.setUserName("alex");
			login.setWechatId("1");
			session.setAttribute(CommonConstant.CLIENT_LOGIN_NAME, login);
		}
		 */	
		
		
		if(logger.isDebugEnabled()){
			StringBuilder sb = new StringBuilder();
			Enumeration<String> enu = req.getParameterNames();
			while(enu.hasMoreElements()){
				String hstr  = enu.nextElement()  ;
				sb.append(hstr+":"+req.getParameter(hstr)+";");
			}
			logger.debug(requestPath+"==>"+sb.toString());
		}
		
		
		////user-agent  MicroMessenger
		String openId  = (String)session.getAttribute(CommonConstant.WEIXING_OPEN_ID);
		String useragent = req.getHeader("user-agent");
		if (CommonUtils.isNotEmpty(openId)||ResourceBundle.getBundle("config").getString("weinx_debug").equals("1")) {				// 如果存在openId, 则认为是微信方式
			filterChain.doFilter(request, response);
		}else{
			//微信浏览器打开
			if(useragent.toLowerCase().indexOf("micromessenger")!=-1){
				/*if(requestPath.contains("register.do")){
					res.sendRedirect("../weixin/common_menu.do?state=1");
				}else if(requestPath.contains("login.do")){
					res.sendRedirect("../weixin/common_menu.do?state=2");
				}else if(requestPath.contains("racehome.do")){
					res.sendRedirect("../weixin/common_menu.do?state=6");
				}else if(requestPath.contains("rank.do")){
					res.sendRedirect("../weixin/common_menu.do?state=8");
				}else if(requestPath.contains("wheel.do")){
					res.sendRedirect("../weixin/common_menu.do?state=9");
				}else if(requestPath.contains("wx_share.do")){
					res.sendRedirect("../weixin/common_menu.do?state=10");
				}else if(requestPath.contains("nmq/index.do")){
					res.sendRedirect("../weixin/common_menu.do?state=11");
				}else if(requestPath.contains("nmq/wheel.do")){
					res.sendRedirect("../weixin/common_menu.do?state=12");
				}else if(requestPath.contains("share.do")){
					res.sendRedirect("../weixin/common_menu.do?state=13");
				}else{
					filterChain.doFilter(request, response);
				}*/
			//非微信打开
			}else{
				filterChain.doFilter(request, response);
			}
		}
	}
	
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		
	}
}